package com.uroad.jilinpda.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.uroad.jilinpda.R;
import com.uroad.jilinpda.adapter.BasePagerAdapter;
import com.uroad.jilinpda.common.BaseActivity;
import com.uroad.jilinpda.modle.CheckVersionMDL;
import com.uroad.jilinpda.modle.OBUOrderInfoMDL;
import com.uroad.jilinpda.net.JsonUtil;
import com.uroad.jilinpda.util.LoginUtil;
import com.uroad.jilinpda.webservice.ActivateWS;
import com.uroad.jilinpda.widget.ChoosePlateColorDialog;
import com.uroad.jilinpda.widget.OBUConfirmDialog;
import com.uroad.jilinpda.widget.TripDialog;
import com.uroad.jilinpda.widget.banner.BannerViewPager;
import com.uroad.jilinpda.widget.banner.PageBean;
import com.uroad.jilinpda.widget.banner.PageHelperListener;
import com.uroad.jilinpda.widget.banner.ZoomIndicator;
import com.uroad.lib.widget.ClearEditText;
import com.uroad.lib.widget.dialog.DialogHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @创建者 wlw
 * @创建时间 2019/6/3 10:34
 * @描述 OBU 激活首页
 */
public class OBUActivationMainActivity extends BaseActivity {
    private static final int REQUEST_CODE = 1001;
    @BindView(R.id.vpTrips)
    BannerViewPager vpTrips;
    @BindView(R.id.Indicator)
    ZoomIndicator Indicator;
    @BindView(R.id.tvTrips)
    TextView tvTrips;
    @BindView(R.id.tvPlateNo)
    TextView tvPlateNo;
    @BindView(R.id.etPhone)
    ClearEditText etPhone;
    @BindView(R.id.tvPlateColor)
    TextView tvPlateColor;
    private List<Integer> tripImgs;
    private int[] tripTexts = new int[]{
            R.string.obu_install_trips_1,
            R.string.obu_install_trips_2,
            R.string.obu_install_trips_3,
            R.string.obu_install_trips_4};
    private List<View> tripViews;
    private BasePagerAdapter adapter;
    private OBUConfirmDialog confirmDialog;
    private String isgm = "";
    private String orderno = "";
    private String isneedpic = "";
    private int index = 0;
    private int userchoose;
    private String[] choose;
    private String plateNo = "";
    private String plateColor = "";
    private String vehtype = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentLayout(R.layout.activity_obu_activation_main);
        ButterKnife.bind(this);
        setTitle("标签激活");
//        setRightBtn("我的待激活", R.color.transparent);
        initView();
        initData();
        setTrips();
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            plateNo = bundle.getString("plateno", "");
            plateColor = bundle.getString("platecolor", "");
            if (!TextUtils.isEmpty(plateNo)) {
                tvPlateNo.setText(plateNo);
                etPhone.setText(plateColor);
            }
          /*  if (!TextUtils.isEmpty(plateColor)) {
                userchoose = Integer.parseInt(plateColor);
                if (userchoose < choose.length) {
                    tvPlateColor.setText(choose[userchoose]);
                }
            }*/
        }
    }


    /*@Override
    protected void onRightClick(View v) {
        super.onRightClick(v);
        Intent intent = new Intent(this, OBUActivationOrderActivity.class);
        startActivityForResult(intent,REQUEST_CODE);
    }*/

    private void initView() {
        choose = new String[]{"蓝", "黄", "黑", "白", "渐变绿", "黄绿双拼", "蓝白渐变"};
        tripImgs = new ArrayList<>();
        tripImgs.add(R.mipmap.ic_obu_install_trips_1);
        tripImgs.add(R.mipmap.ic_obu_install_trips_2);
        tripImgs.add(R.mipmap.ic_obu_install_trips_3);
        tripImgs.add(R.mipmap.ic_obu_install_trips_4);
        Indicator.setSelector(R.drawable.indicator_gray_shape);
        PageBean pageBean = new PageBean.Builder<Integer>()
                .setDataObjects(tripImgs)
                .setIndicator(Indicator)
                .builder();
        vpTrips.setPageListener(pageBean, R.layout.loop_layout, new PageHelperListener<Integer>() {
            @Override
            public void getItemView(View view, Integer data) {
                ImageView imageView = (ImageView) view.findViewById(R.id.loop_banner);
                imageView.setImageResource(data);
            }
        });
        vpTrips.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position % tripTexts.length;
                if (index < tripTexts.length) {
                    setTrips();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        confirmDialog = new OBUConfirmDialog(this);
        confirmDialog.setOnDilaogClickListener(new OBUConfirmDialog.OnDilaogClickListener() {
            @Override
            public void onCancel() {
            }

            @Override
            public void onConfirm() {
                toNextStep();
            }
        });
    }

    private void toNextStep() {
        if (!TextUtils.isEmpty(isgm)) {
            Bundle bundle = new Bundle();
            bundle.putBoolean("isgm", isgm.equals("0") ? false : true);
            bundle.putString("orderno", orderno);
            bundle.putString("isneedpic", isneedpic);
            bundle.putString("vehtype", vehtype);
            openActivity(OBUActivationStep1Activity.class, bundle);
        }
    }

    private void setTrips() {
        tvTrips.setText(Html.fromHtml("<font color ='#333333'>step"+(index+1)+
                "</font><small><font color= '#777777'>/4&nbsp;&nbsp;</font></small><br/>"+
                "<font color ='#333333'>"+getResources().getString(tripTexts[index])+"</font>"));
    }

    @OnClick({R.id.btnQuery,R.id.tvPlateNo,R.id.tvPlateColor})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnQuery://0：蓝1：黄2：黑3：白4：渐变绿5：黄绿双拼6：蓝白渐变
                plateNo = tvPlateNo.getText().toString().trim();
//                String plateColor = tvPlateColor.getText().toString().trim();
                String phone = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(plateNo)) {
                    showShortToast("请输入车牌号");
                    return;
                }
                if (!LoginUtil.isMobileNO(phone)) {
                    showShortToast("请输入办理时提交的手机号");
                    return;
                }
                DialogHelper.showLoading(this,"正在查询");
//                doAllUrlRequest(ActivateWS.fetchActivateskd, ActivateWS.fetchActivateskdParams(plateNo, userchoose + ""), ActivateWS.fetchActivateskd);
                doRequest(ActivateWS.fetchActivates, ActivateWS.fetchActivatesParams(plateNo, phone), ActivateWS.fetchActivates);
                //                openActivity(OBUActivationStep1Activity.class);
                break;
            case R.id.tvPlateNo:
                ChoosePlateColorDialog plateColorDialog = new ChoosePlateColorDialog(this, "2","").setOnDatePickerListener(new ChoosePlateColorDialog.OnDatePickerListener() {
                    @Override
                    public void onDatePicker(String plat, int item, String workcode) {
                        tvPlateNo.setText(plat);
                    }
                });
                plateColorDialog.show();
                plateColorDialog.hideColor(true);
                break;
            case R.id.tvPlateColor:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("请选择车牌颜色");
                builder.setSingleChoiceItems(choose, userchoose, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        userchoose = i;
                        tvPlateColor.setText(choose[i]);
                    }
                });
                builder.show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (data != null) {
                OBUOrderInfoMDL orderMDL = (OBUOrderInfoMDL) data.getSerializableExtra("ordermdl");
                if (orderMDL != null) {
                    orderno = orderMDL.getOrderno();
                    isgm = orderMDL.getIsgm();
                    isneedpic = orderMDL.getIsneedpic();
                    tvPlateNo.setText(orderMDL.getNumberplate());
                    toNextStep();
                }
            }
        }
    }

    @Override
    public void OnHttpTaskComplete(String result, String type) {
        super.OnHttpTaskComplete(result, type);
        DialogHelper.endLoading();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(result);
        if (JsonUtil.GetJsonStatu00(jsonObject)) {
            List<OBUOrderInfoMDL> infoMDL = JsonUtil.getArray(jsonObject.getString("data"), OBUOrderInfoMDL.class);
            if (infoMDL != null && infoMDL.size() > 0) {
                for (int i = 0; i < infoMDL.size(); i++) {
                    OBUOrderInfoMDL mdl = infoMDL.get(i);
                    isgm = mdl.getIsgm();
                    orderno = mdl.getOrderno();
                    isneedpic = mdl.getIsneedpic();
                    vehtype = mdl.getVehtype();
                    confirmDialog.setData(mdl.getOrderno(), mdl.getNumberplate(), mdl.getPlatecolor());
                    confirmDialog.show();
                }
            } else {
                showShortToast("您没有需要激活的卡");
            }
        }else{
            showShortToast(JsonUtil.GetErrorString(jsonObject, "msg"));
        }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void OnHttpFailure(String msg) {
        super.OnHttpFailure(msg);
        DialogHelper.endLoading();
    }

    @Override
    public void OnHttpNetWork(String msg) {
        super.OnHttpNetWork(msg);
        DialogHelper.endLoading();
    }
}
